<template>
	<view class="uni-combox" @click="toggleSelector">
		<view class="uni-combox__label" :style="{color: color}">{{inputVal}}</view>
		<uni-icons :color="color" class="uni-combox__input-arrow" type="arrowdown" size="14"></uni-icons>
		<view class="uni-combox__selector" v-if="showSelector">
			<scroll-view scroll-y="true" class="uni-combox__selector-scroll">
				<view class="uni-combox__selector-empty" v-if="candidates.length === 0">
					<text :style="{color: itemColor}">{{emptyTips}}</text>
				</view>
				<view :style="{color: itemColor}" class="uni-combox__selector-item" v-for="(item,index) in candidates" :key="index" @click.stop="onSelectorClick(index)">
					{{item}}
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import uniIcons from '../uni-icons/uni-icons.vue'
	export default {
		name: 'uniCombox',
		components: {
			uniIcons
		},
		props: {
			color: {
				type: String,
				default: "#000000"
			},
			itemColor: {
				type: String,
				default: "#000000"
			},
			candidates: {
				type: Array,
				default () {
					return []
				}
			},
			emptyTips: {
				type: String,
				default: '无匹配项'
			},
			value: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				showSelector: false,
				inputVal: ''
			}
		},
		created() {
			if (this.candidates.length) {
				this.inputVal = this.candidates[0];
			}
		},
		watch: {
			value: {
				handler(newVal) {
					this.inputVal = newVal
				},
				immediate: true
			},
			candidates: {
				handler(newVal) {
					if (newVal && newVal.length) {
						this.inputVal = newVal[0];
					}
				}
			}
		},
		methods: {
			toggleSelector() {
				this.showSelector = !this.showSelector
			},
			onSelectorClick(index) {
				this.inputVal = this.candidates[index]
				this.showSelector = false
				this.$emit('change', index)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.uni-combox {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		height: 40px;
		padding: 0 10px;
		flex-direction: row;
		align-items: center;
		width: 100%;
		position: relative;
		// border-bottom: solid 1px #DDDDDD;
	}

	.uni-combox__label {
		font-size: 14px;
		line-height: 22px;
		margin-right: 4px;
		color: #999999;
		flex: 1;
		text-align: center;
	}

	.uni-combox__input {
		flex: 1;
		font-size: 14px;
		height: 22px;
		line-height: 22px;
	}

	.uni-combox__input-arrow {
	}

	.uni-combox__selector {
		box-sizing: border-box;
		position: absolute;
		top: 42px;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		border-radius: 6px;
		box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
		z-index: 2;
	}

	.uni-combox__selector-scroll {
		max-height: 200px;
		box-sizing: border-box;
	}

	.uni-combox__selector::before {
		content: '';
		position: absolute;
		width: 0;
		height: 0;
		border-bottom: solid 6px #FFFFFF;
		border-right: solid 6px transparent;
		border-left: solid 6px transparent;
		left: 50%;
		top: -6px;
		margin-left: -6px;
	}

	.uni-combox__selector-empty,
	.uni-combox__selector-item {
		/* #ifdef APP-NVUE */
		display: flex;
		/* #endif */
		line-height: 36px;
		font-size: 14px;
		text-align: center;
		border-bottom: solid 1px #DDDDDD;
		margin: 0px 10px;
		overflow: hidden; 
		white-space: nowrap; 
		text-overflow: ellipsis;
	}

	.uni-combox__selector-empty:last-child,
	.uni-combox__selector-item:last-child {
		border-bottom: none;
	}
</style>
